package 树节点输入模板;

import java.io.*;
import java.util.*;

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        // 打印一下
        MainClass.prettyPrintTree(root);
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        list.add(Arrays.asList(1, 2, 3));

        return list;
    }
}


//===================================================================================================

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class MainClass {


    public static void main(String[] args) throws IOException {
        String filePath = "第168场.E:\\idea-project\\leetcode\\race-temp\\src\\main\\java\\树节点输入模板\\test1.txt";
        FileInputStream is = new FileInputStream(new File(filePath));
        BufferedReader in = new BufferedReader(new InputStreamReader(is));

        String line;
        while ((line = in.readLine()) != null) {
            TreeNode root = stringToTreeNode(line);

            List<List<Integer>> ret = new Solution().zigzagLevelOrder(root);

            String out = int2dListToString(ret);

            System.out.print(out);
        }
    }


    public static TreeNode stringToTreeNode(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return null;
        }

        String[] parts = input.split(",");
        String item = parts[0];
        TreeNode root = new TreeNode(Integer.parseInt(item));
        Queue<TreeNode> nodeQueue = new LinkedList<>();
        nodeQueue.add(root);

        int index = 1;
        while (!nodeQueue.isEmpty()) {
            TreeNode node = nodeQueue.remove();

            if (index == parts.length) {
                break;
            }

            item = parts[index++];
            item = item.trim();
            if (!item.equals("null")) {
                int leftNumber = Integer.parseInt(item);
                node.left = new TreeNode(leftNumber);
                nodeQueue.add(node.left);
            }

            if (index == parts.length) {
                break;
            }

            item = parts[index++];
            item = item.trim();
            if (!item.equals("null")) {
                int rightNumber = Integer.parseInt(item);
                node.right = new TreeNode(rightNumber);
                nodeQueue.add(node.right);
            }
        }
        return root;
    }

    public static String integerArrayListToString(List<Integer> nums, int length) {
        if (length == 0) {
            return "[]";
        }

        String result = "";
        for (int index = 0; index < length; index++) {
            Integer number = nums.get(index);
            result += Integer.toString(number) + ", ";
        }
        return "[" + result.substring(0, result.length() - 2) + "]";
    }

    public static String integerArrayListToString(List<Integer> nums) {
        return integerArrayListToString(nums, nums.size());
    }

    public static String int2dListToString(List<List<Integer>> nums) {
        StringBuilder sb = new StringBuilder("[");
        for (List<Integer> list : nums) {
            sb.append(integerArrayListToString(list));
            sb.append(",");
        }

        sb.setCharAt(sb.length() - 1, ']');
        return sb.toString();
    }

    private static void prettyPrintTree(TreeNode node, String prefix, boolean isLeft) {
        if (node == null) {
            System.out.println("Empty tree");
            return;
        }

        if (node.right != null) {
            prettyPrintTree(node.right, prefix + (isLeft ? "│    " : "      "), false);
        }

        System.out.println(prefix + (isLeft ? "└——" : "┌——") + node.val);

        if (node.left != null) {
            prettyPrintTree(node.left, prefix + (isLeft ? "      " : "│    "), true);
        }
    }

    /**
     * 把树以可视化方式打印出来
     *
     * @param node
     */
    public static void prettyPrintTree(TreeNode node) {
        prettyPrintTree(node, "", true);
        // 分割线搞搞
        System.out.println("==================================================================================");
    }

}